package view.component.window
{
import core.Language;
import core.manager.ShopManager;
import core.model.PlayerModel;

import flash.events.Event;
import flash.events.KeyboardEvent;

import framework.view.components.button.ButtonProxy;

import values.item.INumbericItemValue;
import values.item.ItemValue;
import values.shop.ShopItemValue;

import view.component.asset.IconProxy;
import view.component.dialog.AlertDialogProxy;
import view.component.render.ResourceCostItemRender;

/**
 * MultipleBuyWindowProxy
 * @author jliang
 *
 */
public class MultipleBuyWindowProxy extends AlertDialogProxy
{
    //==========================================================================
    //  Class constants
    //==========================================================================
    public static const MAX_LIMIT:int = 99;
    //==========================================================================
    //  Constructor
    //==========================================================================
    public function MultipleBuyWindowProxy()
    {
        super();
    }
    //==========================================================================
    //  Properties
    //==========================================================================
    [Inject]
    public var playerModel:PlayerModel;
    
    protected function get coster():ResourceCostItemRender
    {
        return proxys["coster"];
    }
    
    protected function get plusBtn():ButtonProxy
    {
        return proxys["plusBtn"];
    }
    
    protected function get decBtn():ButtonProxy
    {
        return proxys["decBtn"];
    }
    
    protected function get icon():IconProxy
    {
        return proxys["icon"];
    }
    
    protected function get costItem():INumbericItemValue
    {
        return coster.data;
    }
    
    protected var vo:ShopItemValue;
    
    protected var _countLimit:int = MAX_LIMIT;

    public function get countLimit():int
    {
        return _countLimit;
    }

    public function set countLimit(value:int):void
    {
        _countLimit = value;
        
        skin.countLimitLabel.text = "/ " + countLimit;
    }
    
    public function set buyCount(value:int):void
    {
        skin.countLabel.text = String(value);
        
        costItem.num = Math.ceil(buyCount * vo.nowCostNum);
        costItem.forceUpdate();
        
        yesBtn.enabled = buyCount > 0;
        
        coster.update();
    }
    
    public function get buyCount():int
    {
        return int(skin.countLabel.text);
    }
    
    public function get maxCostNum():Number
    {
        return playerModel.getItemNum(costItem.key);
    }
    //==========================================================================
    //  Overridden methods: AlertDialogProxy
    //==========================================================================
    override protected function get proxyMapping():Array
    {
        return [ 
            { skin:"closeBtn", classRef: ButtonProxy, id:"closeBtn", onClick: clickToCloseHandler },
            { skin:"plusBtn", classRef: ButtonProxy, id:"plusBtn", onClick: clickToPlusHandler },
            { skin:"decBtn", classRef: ButtonProxy, id:"decBtn", onClick: clickToDecHandler },
            { skin:"", classRef: IconProxy, id:"icon" },
            { skin:"coster", classRef: ResourceCostItemRender, id:"coster" }
        ].concat(super.proxyMapping);
    }	
    
    override public function set data(value:*):void
    {
        super.data = value;
        
        skin.countLabel.restrict = "0-9";
        skin.countLabel.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
        
        vo = value;
        
        icon.load(vo.itemValue.template.getIconPathByKey("icon"));
        skin.nameLabel.text = vo.itemValue.name;
        
        skin.buyLabel.text = Language.getText("@multiple_buy_count_label");
        
        var cost:INumbericItemValue = ItemValue.createResource(vo.costItemKey, vo.nowCostNum);        
        coster.data = cost;
        
        buyCount = 0;
        
        checkLimit();
    }
    
    override public function dispose():void
    {
        skin.countLabel.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
        if (icon)
        {
            icon.stopAndRemove();
        }
        super.dispose();
    }
    
    override protected function onYesClick():void
    {
        ShopManager.buy(vo, buyCount);
        
        super.onYesClick();
    }
    //==========================================================================
    //  Protected methods
    //==========================================================================
    protected function checkLimit():void
    {
        //判断当日剩余购买次数
        if (vo.hasDailyLimit)
        {
            countLimit = Math.min(MAX_LIMIT, vo.todayRestNum);
            return;
        }
        
        //判断消耗上限
        countLimit = Math.min(MAX_LIMIT, Math.floor(maxCostNum / vo.nowCostNum));
        
        plusBtn.enabled = (buyCount < countLimit);
        decBtn.enabled = (buyCount > 0);
    }
    //==========================================================================
    //  Event handlers
    //==========================================================================
    protected function clickToPlusHandler():void
    {
        buyCount ++;
        
        var temp:int = buyCount;
        if (temp > countLimit) buyCount = countLimit;
        else buyCount = temp;
        
        checkLimit();
    }
    
    protected function clickToDecHandler():void
    {
        buyCount --;
        
        checkLimit();
    }
    
    protected function keyUpHandler(event:Event):void
    {
        var temp:int = buyCount;
        if (temp > countLimit) buyCount = countLimit;
        else buyCount = temp;
    }
}
}





